---
author: "Cleaning Pew Islam and Christianity in SSA dataset"
---

# Load

```{r}
# load packages
  source("helper-packages.R")

# load wvs
  pewssa_raw <- 
    read_sav("../raw-data/y-multi-pew-islam-christianity-sub-saharan-africa/2020update7.31.sav")
```

# Clean data

```{r}
# declare dates
  pewssa_dates <- 
    tribble(
      ~resp_country_common, ~resp_interview_start_date, ~resp_interview_end_date,
      "South Africa", "Dec. 12, 2008", "Dec. 26, 2008",             
      "Cameroon", "Dec. 11, 2008", "Dec. 24, 2008",             
      "Kenya", "Dec. 18, 2008", "Dec. 27, 2008",             
      "Nigeria", "Feb. 15, 2009", "Feb. 25, 2009",             
      "Tanzania", "Dec. 15, 2008", "Jan. 6, 2009",             
      "Chad", "Dec. 29, 2008", "Jan. 9, 2009",            
      "Botswana", "Dec. 14, 2008", "Jan. 22, 2009",             
      "Mali", "Jan. 14, 2009", "Jan. 24, 2009",             
      "Ghana", "Jan. 17, 2009", "Jan. 30, 2009",             
      "Ethiopia", "Jan. 22, 2009", "Feb. 1, 2009",            
      "Senegal", "Jan. 14, 2009", "Jan. 24, 2009",             
      "Zambia", "Jan. 25, 2009", "Feb. 3, 2009",          
      "Liberia", "Jan. 21, 2009", "Jan. 31, 2009",             
      "Djibouti", "Jan. 30, 2009", "Feb. 6, 2009",             
      "Uganda", "Feb. 4, 2009", "Feb. 12, 2009",             
      "Guinea-Bissau", "Jan. 22, 2009", "Feb. 1, 2009",             
      "Rwanda", "Jan. 21, 2009", "Jan. 31, 2009",             
      "Mozambique", "Mar. 5, 2009", "Mar. 30, 2009", 
      "Congo - Kinshasa", "Feb. 7, 2009", "Apr. 10, 2009") %>% 
    mutate(
      resp_interview_start_date = as.Date(resp_interview_start_date, "%b. %d, %Y"),
      resp_interview_end_date = as.Date(resp_interview_end_date, "%b. %d, %Y"))

# clean
  pewssa_clean <- 
    pewssa_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "Pew: Islam and Christianity in Sub-Saharan Africa",
        
      # round number (character vector, title case)  
        resp_round = "",      
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "pewrsr.ch/2kQml7z",
      
      # survey mode (in-person/phone/internet)
        resp_survey_mode = "in-person",  

      # country (character vector; list of countries as written in original source)
        resp_country_original = to_character(COUNTRY),

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = 
          countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date = NA) %>% 
        left_join(
          pewssa_dates, by = "resp_country_common") %>% 
        mutate(    
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################
      
      # respondent's religion (character vector that corresponds to master list)
        # helpful resource: http://gizemarikan.com/wp-content/uploads/2016/11/BJPOLS-SUPP-APPENDIX.pdf
        resp_religion = 
          dplyr::recode(
            as.character(RELIGrec),
              "1" = "Christian",
              "2" = "Muslim",
              "3" = "Other religion",
            .default = NA_character_),      

      # respondent's religion (character vector that corresponds to master list)
        denom = to_character(denom), # for christians
        Q37 = to_character(Q37), # for muslims
        
        resp_denomination = 
          case_when(
            is.na(denom) ~ Q37,
            TRUE ~denom),        

      # respondent's age (character vector; bins denoted by single dash ["18-25"])
        resp_age = 
          dplyr::recode(
            as.character(q97rec),
              "1" = "18-24",
              "2" = "25-29",
              "3" = "30-34",
              "4" = "35-39",
              "5" = "40-44",
              "6" = "45-49",
              "7" = "50-54",
              "8" = "55-59",
              "9" = "60 or over",
              .default = NA_character_),        
      
      # respondent's education level
        resp_education_original =
          case_when(
            # South Africa     
            Q99SAF == 1 ~ "1. No formal education [No education]",
            Q99SAF == 2 ~ "2. Incomplete primary school [No education]",
            Q99SAF == 3 ~ "3. Complete primary school [Primary]",
            Q99SAF == 4 ~ "4. Some high school/secondary school [Primary]",
            Q99SAF == 5 ~ "5. Matric/complete secondary [Primary]",
            Q99SAF == 6 ~ "6. Artisans certificate obtained [Primary]",
            Q99SAF == 7 ~ "7. Technikon diploma/degree completed [Primary]",
            Q99SAF == 8 ~ "8. University degree completed [College]",
            Q99SAF == 9 ~ "9. Professional [Primary]", 
            Q99SAF == 10 ~ "10. Technical [Primary]",
            Q99SAF == 11 ~ "11. Secretarial [Primary]",
            Q99SAF %in% c(97, 98, 99) ~ NA_character_,
            # Cameroon
            Q99CAM == 0 ~ "0. No formal education [No education]",
            Q99CAM == 1 ~ "1. First year of primary school (SIL) [No education]",
            Q99CAM == 2 ~ "2. Second year of primary school (CP) [No education]",
            Q99CAM == 3 ~ "3. Third year of primary school (CE1) [No education]",
            Q99CAM == 4 ~ "4. Fourth year of primary school (CE2) [No education]",
            Q99CAM == 5 ~ "5. Fifth year of primary school (CM1) [Primary]",
            Q99CAM == 6 ~ "6. Sixth year of primary school (CM2) [Primary]",
            Q99CAM == 7 ~ "7. First year of secondary school [Primary]",
            Q99CAM == 8 ~ "8. Second year of secondary school [Primary]",
            Q99CAM == 9 ~ "9. Third year of secondary school [Primary]",
            Q99CAM == 10 ~ "10. Fourth year of secondary school [Primary]",
            Q99CAM == 11 ~ "11. Fifth year of secondary school [Primary]",
            Q99CAM == 12 ~ "12. Sixth year of secondary school",
            Q99CAM == 13 ~ "13. Seventh year of secondary school [Primary]",
            Q99CAM == 14 ~ "14. 1st and 2nd year university [Primary]",
            Q99CAM == 15 ~ "15. University degree (3rd year university) [College]",
            Q99CAM == 16 ~ "16. Master degree (4th year university) [College]",
            Q99CAM == 17 ~ "17. Second cycle university (DEA/Master/Doctorate) [College]",
            Q99CAM %in% c(98, 99) ~ NA_character_,
            # Kenya           
            Q99KEN == 0 ~ "0. No formal schooling [No education]",
            Q99KEN == 1 ~ "1. First year of primary education [No education]",
            Q99KEN == 2 ~ "2. Second year of primary education [No education]",
            Q99KEN == 3 ~ "3. Third year of primary education [No education]",
            Q99KEN == 4 ~ "4. Fourth year of primary education [No education]",
            Q99KEN == 5 ~ "5. Fifth year of primary education [Primary]",
            Q99KEN == 6 ~ "6. Sixth year of primary education [Primary]",
            Q99KEN == 7 ~ "7. Seventh year of primary education [Primary]",
            Q99KEN == 8 ~ "8. Eighth year of primary education [Primary]",
            Q99KEN == 9 ~ "9. First year of secondary education [Primary]",
            Q99KEN == 10 ~ "10. Second year of secondary education [Primary]",
            Q99KEN == 11 ~ "11. Third year of secondary education [Primary]",
            Q99KEN == 12 ~ "12. Fourth year of secondary education [Primary]",
            Q99KEN == 13 ~ "13. Fifth year of secondary education [Primary]",
            Q99KEN == 14 ~ "14. Sixth year of secondary education [Primary]",
            Q99KEN == 15 ~ "15. First year of technical training (Polytechnic or Diploma colleges) [Primary]",
            Q99KEN == 16 ~ "16. Second year of technical training (Polytechnic or Diploma colleges) [Primary]",
            Q99KEN == 17 ~ "17. Completed technical training (Polytechnic or Diploma colleges) [Primary]",
            Q99KEN == 18 ~ "18. First year of teachers’ training college [Primary]",
            Q99KEN == 19 ~ "19. Second year of teachers’ training college [Primary]",
            Q99KEN == 20 ~ "20. Completed education at teachers’ training college [Primary]", 
            Q99KEN == 21 ~ "21. Some university education [Primary]",
            Q99KEN == 22 ~ "22. Completed university education [College]",
            Q99KEN == 23 ~ "23. Completed post university education [College]",
            Q99KEN %in% c(99) ~ NA_character_,
            # Nigeria          
            Q99NIG == 0 ~ "0. NO FORMAL SCHOOLING [No education]",
            Q99NIG == 1 ~ "1. INFORMAL SCHOOLING ONLY (INCLUDING KORANIC SCHOOLING) [No education]",
            Q99NIG == 2 ~ "2. SOME PRIMARY SCHOOLING [No education]",
            Q99NIG == 3 ~ "3. PRIMARY SCHOOL COMPLETED [Primary]",
            Q99NIG == 4 ~ "4. SOME SECONDARY SCHOOL / HIGH SCHOOL [Primary]",
            Q99NIG == 5 ~ "5. SECONDARY SCHOOL / HIGH SCHOOL COMPLETED [Primary]",
            Q99NIG == 6 ~ "6. POST-SECONDARY QUALIFICATIONS, OTHER THAN UNIVERSITY E.G A D [Primary]",
            Q99NIG == 7 ~ "7. SOME POLYTECHNIC / UNIVERSITY [Primary]",
            Q99NIG == 8 ~ "8. POLYTECHNIC / UNIVERSITY COMPLETED [College]",
            Q99NIG == 9 ~ "9. POST-GRADUATE [College]",
            Q99NIG %in% c(98, 99) ~ NA_character_,
            # Tanzania  
            Q99TAN == 0 ~ "0. No Formal Education [No education]",
            Q99TAN == 1 ~ "1. Incomplete primary school [No education]",
            Q99TAN == 2 ~ "2. Complete primary school [Primary]",
            Q99TAN == 3 ~ "3. Incomplete secondary:Technical/vocational type [Primary]",
            Q99TAN == 4 ~ "4. Complete secondary:Technical/vocational type [Primary]",
            Q99TAN == 5 ~ "5. Incomplete secondary:University preparatory type [Primary]",
            Q99TAN == 6 ~ "6. Complete secondary:University preparatory type [Primary]",
            Q99TAN == 7 ~ "7. Some university-level education,without a degree [Primary]",
            Q99TAN == 8 ~ "8. University- Level education,with a degree [College]",
            Q99TAN %in% c(98, 99) ~ NA_character_,
            # Chad 
            Q99CHA == 0 ~ "0. No formal education [No education]",
            Q99CHA == 1 ~ "1. First year of primary school (SIL) [No education]",
            Q99CHA == 2 ~ "2. Second year of primary school (CP) [No education]",
            Q99CHA == 3 ~ "3. Third year of primary school (CE1) [No education]",
            Q99CHA == 4 ~ "4. Fourth year of primary school (CE2) [No education]",
            Q99CHA == 5 ~ "5. Fifth year of primary school (CM1) [Primary]",
            Q99CHA == 6 ~ "6. Sixth year of primary school (CM2) [Primary]",
            Q99CHA == 7 ~ "7. First year of secondary school [Primary]",
            Q99CHA == 8 ~ "8. Second year of secondary school [Primary]",
            Q99CHA == 9 ~ "9. Third year of secondary school [Primary]",
            Q99CHA == 10 ~ "10. Fourth year of secondary school [Primary]",
            Q99CHA == 11 ~ "11. Fifth year of secondary school [Primary]",
            Q99CHA == 12 ~ "12. Sixth year of secondary school [Primary]",
            Q99CHA == 13 ~ "13. Seventh year of secondary school [Primary]",
            Q99CHA == 14 ~ "14. 1st and 2nd year university [Primary]",
            Q99CHA == 15 ~ "15. University degree (3rd year university) [College]",
            Q99CHA == 16 ~ "16. Master degree (4th year university) [College]",
            Q99CHA == 17 ~ "17. Second cycle university (DEA/Master/Doctorate) [College]",
            Q99CHA %in% c(98, 99) ~ NA_character_,
            # Botswana
            Q99BOT == 0 ~ "0. No schooling [No education]",
            Q99BOT == 1 ~ "1. Some primary school [No education]",
            Q99BOT == 2 ~ "2. Primary school completed [Primary]",
            Q99BOT == 3 ~ "3. Some secondary school [Primary]",
            Q99BOT == 4 ~ "4. Junior secondary completed [Primary]",
            Q99BOT == 5 ~ "5. Senior secondary completed [Primary]",
            Q99BOT == 6 ~ "6. A-Levels completed [Primary]",
            Q99BOT == 7 ~ "7. Artisans certificate obtained [Primary]",
            Q99BOT == 8 ~ "8. College completed [Primary]",
            Q99BOT == 9 ~ "9. University degree completed [College]",
            Q99BOT == 10 ~ "10. Specialised degree completed (like Clinical Psychologist, Specialist, Orthodontist, etc.) [College]",
            Q99BOT == 11 ~ "11. Still at university [Primary]",
            Q99BOT == 12 ~ "12. Diploma [Primary]",
            Q99BOT %in% c(97, 98, 99) ~ NA_character_,
            # Mali             
            Q99MAL == 0 ~ "0. No formal schooling [No education]",
            Q99MAL == 1 ~ "1. First year of primary school (CP1) [No education]",
            Q99MAL == 2 ~ "2. Second year of primary school (CP2) [No education]",
            Q99MAL == 3 ~ "3. Third year of primary school (CE1) [No education]",
            Q99MAL == 4 ~ "4. Fourth year of primary school (CE2) [No education]",
            Q99MAL == 5 ~ "5. Fifth year of primary school (CM1) [Primary]",
            Q99MAL == 6 ~ "6. Sixth year of primary school (CM2) [Primary]",
            Q99MAL == 7 ~ "7. First year of secondary school (6e) [Primary]",
            Q99MAL == 8 ~ "8. Second year of secondary school (5e) [Primary]",
            Q99MAL == 9 ~ "9. Third year of secondary school (4e) [Primary]",
            Q99MAL == 10 ~ "10. Fourth year of secondary school (3e)BEPC [Primary]",
            Q99MAL == 11 ~ "11. First year of secondary education, second cycle (2d) [Primary]",
            Q99MAL == 12 ~ "12. Second year of secondary education, second cycle ( 1ere) [Primary]",
            Q99MAL == 13 ~ "13. Third year of secondary education, second cycle (Final) BAC [Primary]",
            Q99MAL == 14 ~ "14. First and second year of University [Primary]",
            Q99MAL == 15 ~ "15. Third year of University [Primary]",
            Q99MAL == 16 ~ "16. Fourth year of University [College]",
            Q99MAL == 17 ~ "17. Second cycle of University(DEA/DESS/Doctorate) [College]",
            Q99MAL %in% c(98, 99) ~ NA_character_,
            # Ghana 
            Q99GHA == 0 ~ "0. No formal education [No education]",
            Q99GHA == 1 ~ "1. Some Primary/JSS [No education]",
            Q99GHA == 2 ~ "2. Completed Primary/JHS [Primary]",
            Q99GHA == 3 ~ "3. Some Secondary [Primary]",
            Q99GHA == 4 ~ "4. Completed Secondary/SSS [Primary]",
            Q99GHA == 5 ~ "5. Some post Secondary, Technical/Vocational [Primary]",
            Q99GHA == 6 ~ "6. Post Secondary, Technical/Vocational [Primary]",
            Q99GHA == 7 ~ "7. Some University/Tertiary [Primary]",
            Q99GHA == 8 ~ "8. Completed University/Tertiary [College]",
            Q99GHA == 9 ~ "9. Higher/Post Graduate [College]",
            Q99GHA %in% c(98, 99) ~ NA_character_,
            # Ethiopia 
            Q99ETH == 0 ~ "0. No formal schooling [No education]",
            Q99ETH == 1 ~ "1. First year of primary education(Self contend-Grade 1) [No education]",
            Q99ETH == 2 ~ "2. Second year of primary education(Self Contend-Grade 2) [No education]",
            Q99ETH == 3 ~ "3. Third year of primary education(Self Contend-Grade 3) [No education]",
            Q99ETH == 4 ~ "4. Fourth year of primary education(Self Contend- Grade 4) [No education]",
            Q99ETH == 5 ~ "5. First year of elementary and junior secondary (Grade 5) [Primary]",
            Q99ETH == 6 ~ "6. Second year of elementary and junior secondary (Grade 6) [Primary]",
            Q99ETH == 7 ~ "7. Third year of elementary  and junior secondary (Grade 7) [Primary]",
            Q99ETH == 8 ~ "8. Fourth year of elementary and junior secondary (Grade 8) [Primary]",
            Q99ETH == 9 ~ "9. First year of high school (Grade 9) [Primary]",
            Q99ETH == 10 ~ "10. Second year of high school (Grade10) [Primary]",
            Q99ETH == 11 ~ "11. First year of preparatory school (Grade11) [Primary]",
            Q99ETH == 12 ~ "12. Second year of preparatory school (Grade 12) [Primary]",
            Q99ETH == 13 ~ "13. Some education at technikon [Primary]",
            Q99ETH == 14 ~ "14. Completed Technicon [Primary]",
            Q99ETH == 15 ~ "15. Some university or college education [Primary]",
            Q99ETH == 16 ~ "16. Completed University or college [College]",
            Q99ETH == 17 ~ "17. Post University or College education [College]",
            Q99ETH %in% c(98, 99) ~ NA_character_,
            # Senegal  
            Q99SEN == 0 ~ "0. No formal schooling [No education]",
            Q99SEN == 1 ~ "1. First year of primary school (CP1) [No education]",
            Q99SEN == 2 ~ "2. Second year of primary school (CP2) [No education]",
            Q99SEN == 3 ~ "3. Third year of primary school (CE1) [No education]",
            Q99SEN == 4 ~ "4. Fourth year of primary school (CE2) [No education]",
            Q99SEN == 5 ~ "5. Fifth year of primary school (CM1) [Primary]",
            Q99SEN == 6 ~ "6. Sixth year of primary school (CM2) [Primary]",
            Q99SEN == 7 ~ "7. First year of secondary school (6e) [Primary]",
            Q99SEN == 8 ~ "8. Second year of secondary school (5e) [Primary]",
            Q99SEN == 9 ~ "9. Third year of secondary school (4e) [Primary]",
            Q99SEN == 10 ~ "10. Fourth year of secondary school (3e)BEPC [Primary]",
            Q99SEN == 11 ~ "11. First year of secondary education, second cycle (2d) [Primary]",
            Q99SEN == 12 ~ "12. Second year of secondary education, second cycle ( 1ere) [Primary]",
            Q99SEN == 13 ~ "13. Third year of secondary education, second cycle (Final) BAC [Primary]",
            Q99SEN == 14 ~ "14. First and second year of University [Primary]",
            Q99SEN == 15 ~ "15. Third year of University [Primary]",
            Q99SEN == 16 ~ "16. Fourth year of University [College]",
            Q99SEN == 17 ~ "17. Second cycle of University(DEA/DESS/Doctorate) [College]",
            Q99SEN %in% c(98, 99) ~ NA_character_,
            # Zambia 
            Q99ZAM == 0 ~ "0. No formal schooling [No education]",
            Q99ZAM == 1 ~ "1. First year of primary education [No education]",
            Q99ZAM == 2 ~ "2. Second year of primary education [No education]",
            Q99ZAM == 3 ~ "3. Third year of primary education [No education]",
            Q99ZAM == 4 ~ "4. Fourth year of primary education [No education]",
            Q99ZAM == 5 ~ "5. Fifth year of primary education [Primary]",
            Q99ZAM == 6 ~ "6. Sixth year of primary education [Primary]",
            Q99ZAM == 7 ~ "7. Seventh year of primary education [Primary]",
            Q99ZAM == 8 ~ "8. Eighth year of primary education [Primary]",
            Q99ZAM == 9 ~ "9. First year of secondary education [Primary]",
            Q99ZAM == 10 ~ "10. Second year of secondary education [Primary]",
            Q99ZAM == 11 ~ "11. Third year of secondary education [Primary]",
            Q99ZAM == 12 ~ "12. Fourth year of secondary education [Primary]",
            Q99ZAM == 13 ~ "13. Fifth year of secondary education [Primary]",
            Q99ZAM == 14 ~ "14. Sixth year of secondary education [Primary]",
            Q99ZAM == 15 ~ "15. First year of technical training (Polytechnic or Diploma colleges) [Primary]",
            Q99ZAM == 16 ~ "16. Second year of technical training (Polytechnic or Diploma colleges) [Primary]",
            Q99ZAM == 17 ~ "17. Completed technical training (Polytechnic or Diploma colleges) [Primary]",
            Q99ZAM == 18 ~ "18. First year of teachers’ training college [Primary]",
            Q99ZAM == 19 ~ "19. Second year of teachers’ training college [Primary]",
            Q99ZAM == 20 ~ "20. Completed education at teachers’ training college [Primary]",
            Q99ZAM == 21 ~ "21. Some university education [Primary]",
            Q99ZAM == 22 ~ "22. Completed university education [College]",
            Q99ZAM == 23 ~ "23. Completed post university education [College]",
            Q99ZAM %in% c(98, 99) ~ NA_character_,
            # Liberia   
            Q99LIB == 0 ~ "0. None/No Formal Education [No education]",
            Q99LIB == 1 ~ "1. Some Primary/JSS [No education]",
            Q99LIB == 2 ~ "2. Completed Primary/JHS [Primary]",
            Q99LIB == 3 ~ "3. Some Secondary [Primary]",
            Q99LIB == 4 ~ "4. Completed Secondary/SSS [Primary]",
            Q99LIB == 5 ~ "5. Some post Secondary, Technical/Vocational [Primary]",
            Q99LIB == 6 ~ "6. Post Secondary, Technical/Vocational [Primary]",
            Q99LIB == 7 ~ "7. Some University/Tertiary [Primary]",
            Q99LIB == 8 ~ "8. Completed University/Tertiary [College]",
            Q99LIB == 9 ~ "9. Higher [Post Graduate] [College]",
            Q99LIB %in% c(98, 99) ~ NA_character_,
            # Djibouti 
            Q99DJI == 0 ~ "0. No formal schooling [No education]",
            Q99DJI == 1 ~ "1. First year of primary school (CP1)[No education]",
            Q99DJI == 2 ~ "2. Second year of primary school (CP2) [No education]",
            Q99DJI == 3 ~ "3. Third year of primary school (CE1) [No education]",
            Q99DJI == 4 ~ "4. Fourth year of primary school (CE2) [No education]",
            Q99DJI == 5 ~ "5. Fifth year of primary school (CM1) [Primary]",
            Q99DJI == 6 ~ "6. Sixth year of primary school (CM2) [Primary]",
            Q99DJI == 7 ~ "7. First year of secondary school (6e) [Primary]",
            Q99DJI == 8 ~ "8. Second year of secondary school (5e) [Primary]",
            Q99DJI == 9 ~ "9. Third year of secondary school (4e) [Primary]",
            Q99DJI == 10 ~ "10. Fourth year of secondary school (3e)BEPC [Primary]",
            Q99DJI == 11 ~ "11. First year of secondary education, second cycle (2d) [Primary]",
            Q99DJI == 12 ~ "12. Second year of secondary education, second cycle ( 1ere) [Primary]",
            Q99DJI == 13 ~ "13. Third year of secondary education, second cycle (Final) BAC [Primary]",
            Q99DJI == 14 ~ "14. First and second year of University [Primary]",
            Q99DJI == 15 ~ "15. Third year of University [Primary]",
            Q99DJI == 16 ~ "16. Fourth year of University [College]",
            Q99DJI == 17 ~ "17. Second cycle of University(DEA/DESS/Doctorate) [College]",
            Q99DJI %in% c(98, 99) ~ NA_character_,
            # Uganda 
            Q99UGA == 0 ~ "0. NO FORMAL EDUCATION [No education]",
            Q99UGA == 1 ~ "1. INFORMAL SCHOOLING ONLY [No education]",
            Q99UGA == 2 ~ "2. SOME PRIMARY SCHOOL [No education]",
            Q99UGA == 3 ~ "3. PRIMARY SCHOOL COMPLETED [Primary]",
            Q99UGA == 4 ~ "4. SOME SECONDARY SCHOOL / HIGH SCHOOL [Primary]",
            Q99UGA == 5 ~ "5. SECONDARY SCHOOL / HIGH SCHOOL COMPLETED [Primary]",
            Q99UGA == 6 ~ "6. POST-SECONDARY QUALIFICATIONS OTHER THAN UNIVERSITY [Primary]",
            Q99UGA == 7 ~ "7. SOME UNIVERSITY [Primary]",
            Q99UGA == 8 ~ "8. UNIVERSITY COMPLETED [College]",
            Q99UGA == 9 ~ "9. POST-GRADUATE [College]",
            Q99UGA %in% c(98, 99) ~ NA_character_,
            # Guinea-Bissau    
            Q99GUI == 0 ~ "0. No formal schooling [No education]",
            Q99GUI == 1 ~ "1. First year of primary education [No education]",
            Q99GUI == 2 ~ "2. Second year of primary education [No education]",
            Q99GUI == 3 ~ "3. Third year of primary education [No education]",
            Q99GUI == 4 ~ "4. Fourth year of primary education [No education]",
            Q99GUI == 5 ~ "5. Fifth year of primary education [Primary]",
            Q99GUI == 6 ~ "6. Sixth year of primary education [Primary]",
            Q99GUI == 7 ~ "7. First year of secondary school (6e) [Primary]",
            Q99GUI == 8 ~ "8. Second year of secondary school (5e) [Primary]",
            Q99GUI == 9 ~ "9. Third year of secondary school (4e) [Primary]",
            Q99GUI == 10 ~ "10. Fourth year of secondary school (3e) [Primary]",
            Q99GUI == 11 ~ "11. First year of secondary education, second cycle (2e) [Primary]",
            Q99GUI == 12 ~ "12. Second year of secondary education, second cycle ( 1er) [Primary]",
            Q99GUI == 13 ~ "13. Third year of secondary education, second cycle (Final) BAC [Primary]",
            Q99GUI == 14 ~ "14. First and second year of University [Primary]",
            Q99GUI == 15 ~ "15. Third year of University [Primary]",
            Q99GUI == 16 ~ "16. Fourth year of University [College]",
            Q99GUI == 17 ~ "17. Second cycle of University(DEA/DESS/Doctorate) [College]",
            Q99GUI %in% c(98, 99) ~ NA_character_,
            # Rwanda 
            Q99RWA == 0 ~ "0. No formal schooling [No education]",
            Q99RWA == 1 ~ "1. First year of primary school (CP1) [No education]",
            Q99RWA == 2 ~ "2. Second year of primary school (CP2) [No education]",
            Q99RWA == 3 ~ "3. Third year of primary school (CE1) [No education]",
            Q99RWA == 4 ~ "4. Fourth year of primary school (CE2) [No education]",
            Q99RWA == 5 ~ "5. Fifth year of primary school (CM1) [Primary]",
            Q99RWA == 6 ~ "6. Sixth year of primary school (CM2) [Primary]",
            Q99RWA == 7 ~ "7. First year of secondary school (6e) [Primary]",
            Q99RWA == 8 ~ "8. Second year of secondary school (5e) [Primary]",
            Q99RWA == 9 ~ "9. Third year of secondary school (4e) [Primary]",
            Q99RWA == 10 ~ "10. Fourth year of secondary school (3e)BEPC [Primary]",
            Q99RWA == 11 ~ "11. First year of secondary education, second cycle (2d) [Primary]",
            Q99RWA == 12 ~ "12. Second year of secondary education, second cycle ( 1ere) [Primary]",
            Q99RWA == 13 ~ "13. Third year of secondary education, second cycle (Final) BAC [Primary]",
            Q99RWA == 14 ~ "14. First and second year of University [Primary]",
            Q99RWA == 15 ~ "15. Third year of University [Primary]",
            Q99RWA == 16 ~ "16. Fourth year of University [College]",
            Q99RWA == 17 ~ "17. Second cycle of University(DEA/DESS/Doctorate) [College]",
            Q99RWA %in% c(98, 99) ~ NA_character_,
            # Mozambique      
            Q99MOZ == 0 ~ "0. No Formal Education [No education]",
            Q99MOZ == 1 ~ "1. Informal schoolin only [No education]",
            Q99MOZ == 2 ~ "2. Some primary school [No education]",
            Q99MOZ == 3 ~ "3. Primary school completed [Primary]",
            Q99MOZ == 4 ~ "4. Some secondary school/high school [Primary]",
            Q99MOZ == 5 ~ "5. Secondary school/high school [Primary]",
            Q99MOZ == 6 ~ "6. Post secondary qualifications other than university [Primary]",
            Q99MOZ == 7 ~ "7. Some university [Primary]",
            Q99MOZ == 8 ~ "8. University completed [College]",
            Q99MOZ == 9 ~ "9. Post-graduate [College]",
            Q99MOZ %in% c(98, 99) ~ NA_character_,
            # Congo - Kinshasa            
            Q99CON == 1 ~ "1. No formal education [No education]",
            Q99CON == 2 ~ "2. Completed primary education [Primary]",
            Q99CON == 3 ~ "3. Finished the first two years of secondary [Primary]",
            Q99CON == 4 ~ "4. Professional Degree [Primary]",
            Q99CON == 5 ~ "5. Completed Secondary (State Diploma) [Primary]",
            Q99CON == 6 ~ "6. Two years of university (graduate) [Primary]",
            Q99CON == 7 ~ "7. Completed university (Diploma Degree) [College]",
            Q99CON == 8 ~ "8. Master's Degree [College]",
            Q99CON == 9 ~ "9. Doctoral degree [College]",
            Q99CON %in% c(98, 99) ~ NA_character_),    
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female = 
          case_when(
            Q96 == 1 ~ 0,
            Q96 == 2 ~ 1,
            TRUE ~ NA_real_),
      
      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        resp_rural =
          case_when(
            q115rec %in% c(3) ~ 1, # rural
            q115rec %in% c(1:2) ~ 0, # urban, semi-urban 
            TRUE ~ NA_real_),      

    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: Q31; QTEXT: And which comes closer to describing your view? I generally trust people who have different religious values than me, OR I generally do not trust people who have different religious values than me?; ROPTIONS: 1 = I generally trust people who have different religious values than me [=0] + 2 = I generally do not trust people who have different religious values than me [=1]; TARGET: Different religion; TYPE: Trust",
      
      # original response (as character vector)
        resp_soc_dist_1_original = 
          dplyr::recode(
            as.character(Q31),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            Q31 %in% c(1) ~ 0,
            Q31 %in% c(2) ~ 1,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 2 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_2_qinfo = "NUM: Q45; QTEXT: How comfortable would you be if a child of yours someday married a Christian?; ROPTIONS: 1 = Very comfortable [=0] + 2 = Somewhat comfortable [=0] + 3 =	Not too comfortable [=1] + 4 =	Not at all comfortable [=1]; TARGET: Christian; TYPE: Distance, family",

      # original response (as character vector)
        resp_soc_dist_2_original = 
          dplyr::recode(
            as.character(Q45),
            "5" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_2_bin_recode = 
          case_when(
            Q45 %in% c(1:2) ~ 0,
            Q45 %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    #########################  
    ### SOCIAL DISTANCE 3 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_3_qinfo = "NUM: Q46; QTEXT: How comfortable would you be if a child of yours someday married a Muslim?; ROPTIONS: 1 = Very comfortable [=0] + 2 =	Somewhat comfortable [=0] + 3 =	Not too comfortable [=1] + 4 =	Not at all comfortable [=1]; TARGET: Muslim; TYPE: Distance, family",

      # original response (as character vector)
        resp_soc_dist_3_original = 
          dplyr::recode(
            as.character(Q46),
            "5" = NA_character_,
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_3_bin_recode = 
          case_when(
            Q46 %in% c(1:2) ~ 0,
            Q46 %in% c(3:4) ~ 1,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = "NUM: Q9; QTEXT: Generally speaking, would you say that most people can be trusted or that you can’t be too careful in dealing with people?; ROPTIONS: 1 = Most people can be trusted [=0] + 2 = Can't be too careful [=1] + 3 = Other/Depends [=0]",

      # original response (as character vector)
        resp_gentrust_original = 
          dplyr::recode(
            as.character(Q9),
            "8" = NA_character_,
            "9" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = 
          case_when(
            Q9 %in% c(1, 3) ~ 0,
            Q9 == 2 ~ 1,
            TRUE ~ NA_real_),    
    
    #########################  
    ###### RELIGIOSITY ######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = "NUM: Q42; QTEXT: How important is religion in your life – very important, somewhat important, not too important, or not at all important?; ROPTIONS: 1 = Very important + 2 = Somewhat important + 3 = Not too important + 4 = Not at all important",
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = 
          dplyr::recode(
            as.numeric(Q42),
            `8` = NA_real_,
            `9` = NA_real_),       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = (4 - resp_religiosity_original)/3
    
    ) %>% 
    select(starts_with("resp_"))
```

# Save data

```{r}
  saveRDS(pewssa_clean, "../cleaned-data/y-13-multi-pew-islam-christianity-sub-saharan-africa.rds")
```
